package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"strconv"
)

func main() {

	// 生成1到100的随机整数
	randomNumber := rand.Intn(100) + 1
	var guess int = -1
	var count int = 0
	for guess != randomNumber {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("请输入你猜的数字(1-100内)：")
		text, _, err := reader.ReadLine()
		if err != nil {
			fmt.Println("读取控制台输入失败:", err)
			return
		}
		guess, err = strconv.Atoi(string(text))
		if err != nil {
			fmt.Println("请输入合法数字:", err)
			return
		}
		count++
		if guess == randomNumber {
			fmt.Println("恭喜你，你猜对了！本次猜测次数：", count)
		} else if guess > randomNumber {
			fmt.Println("猜大了...")
		} else if guess < randomNumber {
			fmt.Println("猜小了...")
		}
	}
}
